/**
 * 用户模拟
 * Created by fanGe on 2019-02-21 09:44:57.
 * 自动生成
 */
import Mock from 'mockjs'
let UUID = require('es6-uuid')

export const UserList = []
const count = 10
UserList.push(
  {
    mail: 'asdf@a.cn',
    sysStatus: true,
    mobile: '13568885488',
    password: '123456',
    nikeName: '系统管理员',
    wechatId: 'wechatid1',
    id: 1,
    userId: 'admin'
  },
  {
    mail: 'tommylee@leebing.com',
    sysStatus: true,
    mobile: '',
    nikeName: '李兵',
    password: '123456',
    wechatId: 'wechatid2',
    id: 2,
    userId: 'leebing'
  },
  {
    mail: 'test@leebing.com',
    sysStatus: true,
    mobile: '',
    nikeName: 'TEST',
    password: '123456',
    wechatId: 'wechatid3',
    id: 3,
    userId: 'TEST'
  },
  {
    mail: '519783074@qq.com',
    sysStatus: true,
    mobile: '15882439084',
    nikeName: '贾从雪',
    password: '123456',
    wechatId: 'wechatid4',
    id: 4,
    userId: 'jiacx'
  }
)

for (let i = UserList.length; i < count; i++) {
  UserList.push(Mock.mock({
    id: i + 1,
    mail: '@email',
    userId: '@name',
    mobile: 1381234567 + i,
    avatar: '@image',
    nikeName: '@cname',
    wechatId: '@word(4,8)',
    password: '@word(6, 6)',
    sysStatus: '@boolean'
  }))
}

/**
 * 根据关键字查询用户
 * @param {Object} keys 查询关键字
 * @returns {Array.<*>} lists
 */
export function getUserList (keys) {
  let r = UserList.filter(item => {
    if (keys && item.nikeName.indexOf(keys) < 0) {
      return false
    }
    return item.sysStatus // 只返回有效用户
  })
  return r
}

export function login (account, password) {
  console.log('登录帐号：' + account + ',密码:' + password)
  let r = UserList.some(user => {
    if (user.password !== password) {
      return false
    }
    return user.userId === account || user.mobile === account || user.mail === account
  })
  if (r) {
    return UUID(16, 32)
  } else {
    return null
  }
}

/**
 * 模拟筛选数据
 * @param {Object} paras 请求参数
 * @returns {{totalRow, data}} pages
 */
export function getUserPages (paras) {
  const {keys, pageNumber = 1, pageSize = 10} = paras
  let mockList
  if (keys) {
    mockList = UserList.filter(item => {
      return item.name.indexOf(keys) !== -1 || item.nikeName.indexOf(keys) !== -1 || item.userId.indexOf(keys) !== -1 || item.mail.indexOf(keys) !== -1 || item.wechatId.indexOf(keys) !== -1 || item.mobile.indexOf(keys) !== -1
    })
  } else {
    mockList = UserList
  }

  const pageList = mockList.filter((item, index) => index < pageSize * pageNumber && index >= pageSize * (pageNumber - 1))

  return {
    totalRow: mockList.length,
    data: pageList
  }
}

/**
 * 获取用户id
 * @param {Number} id 主键
 * @returns {Object} user
 */
export function getUser (id) {
  const prjs = UserList.filter((item, index) => {
    return item.id === id
  })
  if (prjs && prjs.length > 0) {
    return prjs[0]
  } else {
    return null
  }
}

/**
 * 新增用户
 * @param {Object} user 用户
 * @returns {Object} result
 */
export function createUser (user) {
  user.id = UserList.length
  UserList.push(user)
  return user.id
}

/**
 * 修改用户
 * @param {Object} user 用户
 * @returns {Object} result
 */
export function putUser (user) {
  let index = UserList.findIndex(prj => prj.id === user.id)
  if (index > -1) {
    UserList.splice(index, 1, user)
    return null
  }
  return '未找到用户' + user.id
}
